{include file="common/head"/}
<style>
    .layui-form-label{
    width: 120px !important;
    }
</style>
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>添加工具</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label">工具类型</label>
            <div class="layui-input-4">
                <select name="type_id" lay-verify="required" id="type">
                    <option value="">请选择工具类型</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工具编号</label>
            <div class="layui-input-4">
                <input type="text" name="number" lay-verify="required" placeholder="{:lang('pleaseEnter')}工具编号" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工具名称</label>
            <div class="layui-input-4">
                <input type="text" name="title" lay-verify="required" placeholder="{:lang('pleaseEnter')}工具名称" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工具型号</label>
            <div class="layui-input-4">
                <input type="text" name="model" lay-verify="required" placeholder="{:lang('pleaseEnter')}工具型号" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工具属性</label>
            <div class="layui-input-4">
                <input type="text" name="attr"  placeholder="{:lang('pleaseEnter')}工具属性" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">需校验</label>
            <div class="layui-input-block">
                <input type="radio" name="is_check" value="1" title="是" lay-filter="is_check">
                <input type="radio" name="is_check" value="0" title="否" checked lay-filter="is_check">
            </div>
        </div>

        <div class="layui-form-item checks" style="display: none">
            <label class="layui-form-label">最近校验日期</label>
            <div class="layui-input-4">
                <input type="text" name="check_date" id="date"  placeholder="{:lang('pleaseEnter')}最近校验日期" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
            </div>
        </div>

        <div class="layui-form-item checks" style="display: none">
            <label class="layui-form-label">校验周期</label>
            <div class="layui-input-4">
                <input type="number" name="waring"  placeholder="{:lang('pleaseEnter')}校验周期（单位：天）" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <input type="hidden" name="pic" id="avatar">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="adBtn"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="adPic">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-4">
                <textarea name="desc" placeholder="请输入内容" lay-verify="required" class="layui-textarea"></textarea>
            </div>
            <div class="layui-form-mid layui-word-aux">
                不超过250个汉字。
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <!--<input type="hidden" name="admin_id">-->
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a href="{:url('listUI')}" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script src="/public/static/common/js/jquery.2.1.1.min.js"></script>
<script>


    $.post('{:url("typeList")}',function (dl) {

        $.each(dl,function (k,v) {
            $('#type').append('<option value="'+v.id+'">'+v.type_name+'</option>');
        });

        handle();

    });


    function handle(){
        layui.use(['form', 'layer','upload','laydate'], function () {
            var form = layui.form, layer = layui.layer,$= layui.jquery,upload = layui.upload,laydate  = layui.laydate;
            var info = {$info|raw};
            var url = '';
            if(info == null){
                url = '{:url("add")}';
            }else{
                url = '{:url("edit")}';
            }


            form.val("form", info);
            if(info){
                $('#adPic').attr('src',info.avatar);
            }
            form.render();
            form.on('submit(submit)', function (data) {
                loading =layer.load(1, {shade: [0.1,'#fff']});

                if(info != null){
                    data.field.id = info.id;
                }

                delete data.field.file;

                $.post(url, data.field, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                            //location.href = location.href;

                        });
                    } else {
                        layer.msg(res.msg, {time: 1800, icon: 2});
                    }
                });
            });


            form.on('radio(is_check)', function(data){

                if(data.value == '1'){
                    $('.checks').show();
                }else{
                    $('.checks').hide();
                }

            });

            laydate.render({
                elem: '#date' //指定元素
            });

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#adBtn'
                ,url: '{:url("UpFiles/upload")}'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#adPic').attr('src', result); //图片链接（base64）
                    });
                },
                done: function(res){
                    if(res.code>0){
                        $('#avatar').val(res.url);
                    }else{
                        //如果上传失败
                        return layer.msg('上传失败');
                    }
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

        });
    }


</script>